package inabit.fuzzy.inference;

import java.util.ArrayList;
import java.util.List;

import inabit.base.RuleBase;
import inabit.base.WorkingMemory;
import inabit.data.FireableRule;
import inabit.data.Rule;
import inabit.data.expression.ConsequentExpression;
import inabit.inference.ForwardEngine;
/**Inference engines that use many-valued logic and which rules
 * are ordered at the conflict set so that it will not generate inconsistency 
 * during rule firing
 * @author Rafael Rocha*/
public class FuzzyForwardEngine extends ForwardEngine {

	@Override
	protected List<FireableRule> conflictSet(RuleBase rb, WorkingMemory wm) {
		List<FireableRule> conflictSet = new ArrayList<FireableRule>();
		for (FireableRule r : rb.getAllFireableRules()) {
			Double d = r.getPrecedent().evaluate(wm);
			if (!r.hasFired() && d != null && d > Double.MIN_VALUE) {
				conflictSet.add(r);
			}
		}
		return conflictSet;
	}

	@Override
	protected FireableRule resolveSet(List<FireableRule> conflictSet, WorkingMemory wm) {
		return conflictSet.get(0);
	}

	@Override
	protected String getRootNodeLabel() {
		return ">>>";
	}

	@Override
	protected String getInnerNodeLabel(Rule r) {
		// TODO Auto-generated method stub
		return r.getID();
	}

	@Override
	protected String getConsequentString(ConsequentExpression consequent) {
		// TODO Auto-generated method stub
		return consequent.toString();
	}

}
